#include "pch.hpp"
#include "ErrorWidget.hpp"

using namespace Engine;
using namespace Engine::Edit;




ErrorWidget::ErrorWidget(QWidget* parent)
	: QWidget(parent)
	, m_state(State::Idle)

	, m_modelTest(&m_model)
{
	m_proxyModel.setSourceModel(&m_model);
	m_proxyModel.setDynamicSortFilter(true);
	m_proxyModel.setFilterKeyColumn(0);
	m_proxyModel.setFilterRole(Qt::UserRole);

	ui.setupUi(this);
	ui.tableView->setModel(&m_proxyModel);

	verify(connect(&m_model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), SLOT(modelChanged())));
	verify(connect(&m_model, SIGNAL(rowsInserted(const QModelIndex&, int, int)), SLOT(modelChanged())));
	verify(connect(&m_model, SIGNAL(rowsRemoved(const QModelIndex&, int, int)), SLOT(modelChanged())));

	verify(connect(ui.errorButton, SIGNAL(clicked()), SLOT(updateLevels())));
	verify(connect(ui.warningButton, SIGNAL(clicked()), SLOT(updateLevels())));
	verify(connect(ui.infoButton, SIGNAL(clicked()), SLOT(updateLevels())));
	verify(connect(ui.debugButton, SIGNAL(clicked()), SLOT(updateLevels())));

	updateLevels();
}
///////////////////////////////////////////////////////////////////////////////////////////////////

ErrorWidget::~ErrorWidget()
{}
///////////////////////////////////////////////////////////////////////////////////////////////////




void ErrorWidget::changeState(State::Type state)
{
	m_state = state;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void ErrorWidget::add(ErrorModel::const_ptr model)
{
	m_model.add(model);
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void ErrorWidget::remove(ErrorModel::const_ptr model)
{
	m_model.remove(model);
}
///////////////////////////////////////////////////////////////////////////////////////////////////




void ErrorWidget::modelChanged()
{
	// We have to update the text of the buttons according to the number
	// of errors, warnings, etc...

	int count = m_model.rowCount(logger::entry::error);
	ui.errorButton->setText(count == 1 ? tr("1 Error") : tr("%1 Errors").arg(count));

	count = m_model.rowCount(logger::entry::warning);
	ui.warningButton->setText(count == 1 ? tr("1 Warning") : tr("%1 Warnings").arg(count));

	count = m_model.rowCount(logger::entry::info);
	ui.infoButton->setText(count == 1 ? tr("1 Info Message") : tr("%1 Info Messages").arg(count));

	count = m_model.rowCount(logger::entry::debug);
	ui.debugButton->setText(count == 1 ? tr("1 Debug Message") : tr("%1 Debug Messages").arg(count));
}
///////////////////////////////////////////////////////////////////////////////////////////////////





void ErrorWidget::restoreSettings(QSettings& settings)
{
	if(settings.contains("headerView"))
		ui.tableView->horizontalHeader()->restoreState(settings.value("headerView").toByteArray());

	if(settings.contains("errors")) ui.errorButton->setChecked(settings.value("errors").toBool());
	if(settings.contains("warnings")) ui.warningButton->setChecked(settings.value("warnings").toBool());
	if(settings.contains("infos")) ui.infoButton->setChecked(settings.value("infos").toBool());
	if(settings.contains("debug")) ui.debugButton->setChecked(settings.value("debug").toBool());

	updateLevels();
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void ErrorWidget::saveSettings(QSettings& settings) const
{
	// We'll save both the selection as well as the table's layout (column size)
	settings.setValue("headerView", ui.tableView->horizontalHeader()->saveState());
	settings.setValue("errors", ui.errorButton->isChecked());
	settings.setValue("warnings", ui.warningButton->isChecked());
	settings.setValue("infos", ui.infoButton->isChecked());
	settings.setValue("debug", ui.debugButton->isChecked());
}
///////////////////////////////////////////////////////////////////////////////////////////////////




void ErrorWidget::updateLevels()
{
	QSet<QString>   levels;

	if(ui.errorButton->isChecked())
		levels << logger::entry::toString(logger::entry::error);

	if(ui.warningButton->isChecked())
		levels << logger::entry::toString(logger::entry::warning);

	if(ui.infoButton->isChecked())
		levels << logger::entry::toString(logger::entry::info);

	if(ui.debugButton->isChecked())
		levels << logger::entry::toString(logger::entry::debug);

	QString rx;
	foreach(QString str, levels)
	{
		if(!rx.isEmpty())
			rx += "|";

		rx += str;
	}

	m_proxyModel.setFilterRegExp(rx);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
